package org.tea.inventory.admin.web.admin;

import org.tea.inventory.admin.annotation.LoginAdmin;
import org.tea.inventory.base.util.ResponseUtil;
import org.tea.inventory.db.domain.AlumniOrganize;
import org.tea.inventory.db.domain.AlumniUser;
import org.tea.inventory.db.domain.NewsComment;
import org.tea.inventory.db.domain.QuestionsAnswers;
import org.tea.inventory.db.service.AlumniOrganizeService;
import org.tea.inventory.db.service.NewsCommentService;
import org.tea.inventory.db.service.QuestionsAnswersService;
import org.tea.inventory.db.service.UserService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin/dashboard")
public class DashbordController {
    private final Log logger = LogFactory.getLog(DashbordController.class);

    @Autowired
    private UserService userService;

    @Autowired
    private NewsCommentService newsCommentService;

    @Autowired
    private AlumniOrganizeService alumniOrganizeService;

    @Autowired
    private QuestionsAnswersService questionsAnswersService;
//    @Autowired
//    private LitemallGoodsService goodsService;
//    @Autowired
//    private LitemallProductService productService;
//    @Autowired
//    private LitemallOrderService orderService;

    @GetMapping("")
    public Object info(@LoginAdmin Integer adminId){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }
        AlumniUser user = null;
        //校友录数量
        int userTotal = userService.selectCountNotYK();
        //游客数量
        user = new AlumniUser();
        user.setRoleType("游客");
        int visitorsTotal = userService.count(user);
        //待审核数量
        user = new AlumniUser();
        user.setAuditStatus(1);
        int awaitAuditTotal = userService.count(user);
        //昨日浏览人数
        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.DATE,-1);
        Date time=cal.getTime();
        String lastLoginDate = new SimpleDateFormat("yyyy-MM-dd").format(time);
        user = new AlumniUser();
        user.setLastLoginTimeStr(lastLoginDate);
        int yesterdayTotal = userService.count(user);

        //今日评论数量
        NewsComment comment = new NewsComment();
        comment.setCreateDate(new SimpleDateFormat("yyyy-MM-dd").format( new Date()));
        //int todayTotal = newsCommentService.countSelective(comment);

        //待审核圈子数量
        AlumniOrganize organize = new AlumniOrganize();
        organize.setAuthStatus(0);
        //int groupTotal = alumniOrganizeService.countOrganize(organize);

        //提问待回答数量
        QuestionsAnswers questionsAnswers = new QuestionsAnswers();
        questionsAnswers.setIsAnswer(false);
        //int questionTotal = questionsAnswersService.countSelective(questionsAnswers);
//        int goodsTotal = goodsService.count();
//        int productTotal = productService.count();
//        int orderTotal = orderService.count();
        Map<String, Integer> data = new HashMap<>();
        data.put("userTotal", userTotal);
        data.put("visitorsTotal", visitorsTotal);
        data.put("awaitAuditTotal", awaitAuditTotal);
        data.put("yesterdayTotal", yesterdayTotal);
        //data.put("todayTotal",todayTotal);
        //data.put("groupTotal",groupTotal);
        //data.put("questionTotal",questionTotal);
//        data.put("goodsTotal", goodsTotal);
//        data.put("productTotal", productTotal);
//        data.put("orderTotal", orderTotal);

        return ResponseUtil.ok(data);
    }

}
